/*
 * Copyright © 2020-2021 尛飛俠（Denvie） All rights reserved.
 */

package cn.denvie.springboot.freemarker.controller;

import cn.denvie.springboot.freemarker.custom.SortNumberMethod;
import cn.denvie.springboot.freemarker.model.User;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * FreemarkerController。
 *
 * @author Denvie
 * @date 2021/5/25
 * @since 1.0.0
 */
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
    @Autowired
    private freemarker.template.Configuration configuration;

    /**
     * 入门
     */
    @GetMapping("/hello")
    public String heller(Map<String, Object> model) {
        model.put("name", "Denvie");
        model.put("sex", 1);
        return "hello";
    }

    /**
     * 基本用法
     */
    @GetMapping("/basic")
    public String basic(Map<String, Object> model) {
        model.put("intVal", 100);
        model.put("longVal", 10000L);
        model.put("stringVal", "Hello FreeMarker");
        model.put("doubleVal", 3.14d);
        model.put("booleanVal", true);
        model.put("dateVal", new Date());
        model.put("dateNull", null);

        model.put("user", new User("Denvie", "<font color=\"red\">个人描述</font>"));

        model.put("myList", Arrays.asList("Java", "Python", "Vue"));

        Map<String, String> myMap = new HashMap<>();
        myMap.put("Java", "你好Java");
        myMap.put("Python", "你好Python");
        myMap.put("Vue", "你好Vue");
        model.put("myMap", myMap);

        // 添加自定义排序函数
        model.put("sort_num", new SortNumberMethod());

        return "basic";
    }

    /**
     * 自定义指令
     */
    @GetMapping("directive")
    public String directive(Map<String, Object> model) {
        return "directive";
    }

    /**
     * 宏指令
     */
    @GetMapping("macro")
    public String macro(Map<String, Object> model) {
        return "macro";
    }

    /**
     * 函数指令
     */
    @GetMapping("function")
    public String function(Map<String, Object> model) {
        return "function";
    }

    /**
     * 页面静态化
     */
    @GetMapping("static")
    @ResponseBody
    public String staticPage() throws IOException {
        // 加载模板文件
        Template template = configuration.getTemplate("static.html");
        // 构建数据模型
        Map<String, Object> model = new HashMap<>();
        model.put("title", "一勤天下无难事");
        model.put("source", "央视网新闻");
        model.put("publishTime", new Date());
        model.put("content", "胸怀千秋伟业，恰是百年风华。" +
                "历史是勇敢者创造的，时代是奋斗者书写的。中华民族是勤于劳动、善于创造的民族。" +
                "关于勤奋，前人论之多矣，诸如“功崇惟志，业广惟勤”、“人生在勤，勤则不匮”、“业精于勤荒于嬉”等。" +
                "毛泽东《水调歌头·重上井冈山》中的“世上无难事，只要肯登攀”，可说是“一勤天下无难事”的另一种表达。");

        // 生成html文件
        File storeDir = new File(System.getProperty("user.dir"), "temp");
        if (!storeDir.exists()) {
            storeDir.mkdirs();
        }
        String fileName = "news_" + System.currentTimeMillis() + ".html";
        File htmlFile = new File(storeDir, fileName);
        try (FileWriter fileWriter = new FileWriter(htmlFile)) {
            template.process(model, fileWriter);
        } catch (TemplateException e) {
            return "failure: " + e.getMessage();
        }
        return "success";
    }
}
